<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50"
      >
      <ArticleItem v-for="item in liebiao" :key="item.art_id" :obj="item" @click.native="$router.push(`/article/${item.art_id}`)" :ishowx="false"></ArticleItem>
      </van-list>
    </div>
</template>

<script>
import { searchResultListAPI } from '@/api/search.js'
import ArticleItem from '@/views/Home/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  data() {
    return {
      liebiao: [],
      page: 1,
      loading: false, // 加载状态
      finished: false // 全部加载完成状态
    }
  },
  components: {
    ArticleItem
  },
  async created() {
    this.getmoreapi()
  },
  methods: {
    onLoad() {
      this.page++
      this.getmoreapi()
    },
    async getmoreapi() {
      const resp = await searchResultListAPI({
        q: this.$route.params.keywords,
        page: this.page
      })
      if (resp.data.data.results === 0) {
        this.finished = true
        return
      }
      this.liebiao = [...this.liebiao, ...resp.data.data.results]
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
.van-nav-bar{
  background: linear-gradient(to right, #654ea3, #eaafc8);
}
</style>
